<!--
  * 下料表
  *
  * @Author:    zzx
  * @Date:      2025-08-08 19:37:29
  * @Copyright  zzx
-->
<template>
  <a-modal
      :title="form.cuttingId ? '编辑' : '添加'"
      :width="800"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-alert :message="'报价名称：' + form.quotationName" type="info" show-icon/>
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" style="margin-top: 10px">
        <a-form-item label="下料名称"  name="cuttingName">
          <a-input style="width: 100%" v-model:value="form.cuttingName" placeholder="下料名称" />
        </a-form-item>
        <a-form-item label="备注"  name="remark">
          <a-input style="width: 100%" v-model:value="form.remark" placeholder="备注" />
        </a-form-item>
        <a-form-item label="文件"  name="cuttingFile">
          <FileUpload
              :defaultFileList="form.cuttingFile"
              :folder="FILE_FOLDER_TYPE_ENUM.COMMON.value"
              buttonText="上传 文件"
              listType="text"
              @change="e => form.cuttingFile = e"
          />
        </a-form-item>
        <a-form-item label="预计完成时间"  name="exceptCompleteTime">
          <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.exceptCompleteTime" style="width: 100%" placeholder="预计完成时间" />
        </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { cuttingInfoApi } from '/src/api/business/project/cutting-info-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import FileUpload from '/@/components/support/file-upload/index.vue';
  import { FILE_FOLDER_TYPE_ENUM } from '/@/constants/support/file-const';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    if (!rowData.cuttingId) {
      console.error(`新增下料单 ～～～  ${rowData.quotationName}`)
      form.cuttingName = rowData.quotationName
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      projectId: undefined, //项目id
      quotationId: undefined, //报价id
      cuttingId: undefined, //报价id
      cuttingDateId: undefined, //下料编号
      cuttingName: undefined, //下料名称
      remark: undefined, //备注
      cuttingFile: undefined, //文件
      exceptCompleteTime: undefined, //预计完成时间
  };

  let form = reactive({ ...formDefault });

  const rules = {
    cuttingName: [{ required: true, message: '下料名称 必填' }],
    cuttingFile: [{ required: true, message: '下料文件 必填' }],
    remark: [{ required: true, message: '备注 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.cuttingId) {
        await cuttingInfoApi.update(form);
      } else {
        await cuttingInfoApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
